# -*- coding=utf-8 -*-

'''
主要参考：【requests中会话cookie的增加、更新、删除】http://blog.csdn.net/titiyufeng/article/details/53182951?locationNum=5&fps=1

'''
import requests
import urllib.parse
import time

def get_cookies_baidu():
    s = requests.session()
    r = s.get('https://www.baidu.com')
    print(s.cookies.get_dict())  # 输出cookies

def get_cookies_yaozhi():
    province_name = urllib.parse.quote("江苏省")
    grade = urllib.parse.quote("全部")
    type = urllib.parse.quote("全部")
    base_url = "https://db.yaozh.com/"
    # param = "hmap?grade=%s&p=%d&pageSize=%d&province=%s&type=%s" % (grade, p, pagesize, province_name, type)
    param = "hmap?name=&grade=%s&address=&type=%s&bedstr=&bedend=&province=%s" % (grade, type, province_name)
    target_url = base_url + param
    print(target_url)
    s = requests.session()
    r = s.get(target_url)
    print(s.cookies.get_dict())  # 输出cookies

def add_cookies_test():
    url = 'http://httpbin.org/cookies'
    cookies = dict(PHPSESSID='123456789', HI_COOKIE='KEY_123456789')
    r = requests.get(url, cookies=cookies)
    print(r.text)

def add_cookies_yaozhi():
    province_name = urllib.parse.quote("江苏省")
    grade = urllib.parse.quote("全部")
    type = urllib.parse.quote("全部")
    base_url = "https://db.yaozh.com/"
    # param = "hmap?grade=%s&p=%d&pageSize=%d&province=%s&type=%s" % (grade, p, pagesize, province_name, type)
    param = "hmap?name=&grade=%s&address=&type=%s&bedstr=&bedend=&province=%s" % (grade, type, province_name)
    target_url = base_url + param
    print(target_url)

    #构造cookie
    cookies = dict(PHPSESSID='123456789', HI_COOKIE='KEY_123456789')


    s = requests.session()
    r = s.get(target_url)
    print(s.cookies.get_dict())  # 输出cookies
    print(r.text)
    print(r.cookies)

if __name__== '__main__':
    time_start=time.time()
    #print("test")
    #get_cookies_baidu()
    #get_cookies_yaozhi()
    #add_cookies_test()
    add_cookies_yaozhi()
    print("共耗时"+str(time.time()-time_start)+"s")